unit clsDepartamento;

interface

uses Dm, dbtables, biblio;

type
  TDepartamento = class(TObject)
  public
    Dep_descricao: string;
    Vl_HoraH: double;
    Vl_HoraM: double;
    Data_Atual: TDateTime;
    Responsavel: string;
    Dep_Ordem: integer;
    Dep_Abreviacao: string;
    function Gravar(p_Dep_codigo: string): Byte;
    function Recuperar(p_Dep_codigo: string): Byte;
    function Excluir(p_Dep_codigo: string): Byte;
    function Recuperar_Maquina(p_Dep_codigo: string): string;
    procedure Atualiza_Horas(p_Dep_codigo: string);
    procedure Consultar(p_nome: string; var p_q: TQuery);
  end;

implementation

function TDepartamento.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM Departamento where Dep_codigo=:k_Dep_codigo');
  tq.parambyname('k_Dep_codigo').asstring := strzero(p_Dep_codigo, 2);
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO Departamento VALUES(:k_Dep_codigo,:k_Dep_descricao,null,null,null,:k_responsavel,:k_Dep_Ordem,:k_Dep_Abreviacao)');
  end else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE Departamento SET Dep_descricao=:k_Dep_descricao,Responsavel=:k_responsavel,Dep_Ordem=:k_Dep_Ordem,Dep_Abreviacao=:k_Dep_Abreviacao WHERE Dep_codigo=:k_Dep_codigo');
  end;
  tq.parambyname('k_Dep_codigo').asstring := strzero(p_Dep_codigo, 2);
  tq.parambyname('k_Dep_descricao').asstring := Dep_descricao;
  tq.parambyname('k_responsavel').asstring := Responsavel;
  tq.parambyname('k_Dep_Ordem').asinteger := Dep_Ordem;
  tq.parambyname('k_Dep_Abreviacao').asstring := Dep_Abreviacao;
  tq.close;
  tq.execsql;
  result := 1;
  tq.Free;
end;

function TDepartamento.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM Departamento where Dep_codigo=:k_Dep_codigo');
  tq.Params[0].asstring := strzero(p_Dep_codigo, 2);
  tq.open;
  if tq.eof then Result := 0 else Result := 1;
  Dep_descricao := tq.fieldbyname('Dep_descricao').asstring;
  Vl_HoraH := tq.fieldbyname('Vl_HoraH').asfloat;
  Vl_HoraM := tq.fieldbyname('Vl_HoraM').asfloat;
  Data_Atual := tq.fieldbyname('Data_Atual').asdatetime;
  Responsavel := tq.fieldbyname('Responsavel').asstring;
  Dep_Ordem := tq.fieldbyname('Dep_Ordem').asinteger;
  Dep_Abreviacao := tq.fieldbyname('Dep_Abreviacao').asstring;
  tq.Close;
  tq.Free;
end;

function TDepartamento.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM Departamento WHERE Dep_codigo=:k_Dep_codigo');
  tq.parambyname('k_Dep_codigo').asstring := strzero(p_Dep_codigo, 2);
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;
end;

procedure TDepartamento.Consultar;
begin
  p_q.close;
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select * from Departamento where Dep_descricao>=:k_Dep_descricao order by Dep_descricao');
  p_q.parambyname('k_Dep_descricao').asstring := p_nome;
end;

procedure TDepartamento.Atualiza_Horas;
var
  tq: TQuery;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := false;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Update Departamento set Vl_HoraH=:a,Vl_HoraM=:b,Data_Atual=:c where ' +
    'Dep_Codigo=:d');
  tq.params[0].asfloat := Vl_HoraH;
  tq.params[1].asfloat := Vl_HoraM;
  tq.params[2].asdatetime := Data_Atual;
  tq.params[3].asstring := p_Dep_Codigo;
  tq.execsql;
  tq.close;
  tq.free;
end;

function TDepartamento.Recuperar_Maquina;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from Maquina where Dep_codigo=:a');
  tq.Params[0].asstring := strzero(p_Dep_codigo, 2);
  tq.open;
  Result := tq.fieldbyname('Cod_Maquina').asstring;
  tq.Close;
  tq.Free;
end;

end.
